const app = getApp();
const fileSystemManager = wx.getFileSystemManager()
import { handleIdCardProcessImg } from "../../common/api";

Page({
  data: {
    src1: "/images/icon_card_2.png",
    src2: "/images/icon_card_1.png",
    hideSide1: false,
    hideSide2: false,
    showPop: false,
    printer_name: "未选择打印机"
  },
  onLoad: function (options) {
    this.setData({
      showPop: !app.globalData.idCardArgee
    });
  },
  onShow: function () {
    //已选择图片
    if (typeof this.data.side !== "undefined") {
      let that = this
      wx.getStorage({
        key: 'takePhoto',
        success: res => {
          console.log("照片裁切完成返回", res)
          let side = that.data.side
          if (res.data.done) {
            wx.compressImage({
              src: res.data.path, // 图片路径
              quality: 80, // 压缩质量
              success: res => {
                //压缩照片
                that.setData({
                  [`src${side}`]: res.tempFilePath,
                  [`hideSide${side}`]: true
                });
                wx.removeStorageSync('takePhoto');
              }

            })
          }
        }
      })
    }
    let printer = app.globalData.printer;
    if (printer == undefined) {
      wx.showModal({
        title: "提示",
        content: "请先选择打印机",
        showCancel: false,
        confirmText: "去选择",
        success: res2 => {
          if (res2.confirm) {
            wx.navigateTo({
              url: '/pages/shop/index',
            })
          }
        }
      })
    } else {
      this.setData({
        printer_name: `${app.globalData.printer.printerName}(${app.globalData.printer.shopName})`,
        printer: app.globalData.printer,
      })
    }


  },
  idCardSelect(e) {
    let side = e.currentTarget.dataset.side;
    this.setData({
      side: side
    })
    wx.chooseMedia({
      count: 1,
      success: res => {
        console.log("选择图片成功", res)
        wx.setStorage({
          data: {
            path: res.tempFiles[0]["tempFilePath"],
            size: "id_card",
            done: false,
          },
          key: 'takePhoto',
          success: _ => {
            wx.navigateTo({
              url: '/pages/photo_resize/index',
            })
          }
        })
      }
    })
  },
  //合成身份证
  process() {
    if (this.data.hideSide1 && this.data.hideSide2) {
      wx.showLoading({
        title: '正在合成图片...',
        mask: true,
      })
      let that = this;
      let image1;
      let image2;
      fileSystemManager.readFile({
        filePath: that.data.src1, // 例如图片临时路径
        encoding: 'base64',
        success(res) {
          image1 = res.data
          fileSystemManager.readFile({
            filePath: that.data.src2, // 例如图片临时路径
            encoding: 'base64',
            success(res) {
              image2 = res.data
              let data = {
                idOneImage: image1,
                idTwoImage: image2
              }
              console.log(data)
              handleIdCardProcessImg(data).then(res => {
                wx.hideLoading()
                console.log("合成身份证", res.msg);
                if (res.code == 200) {
                  wx.showToast({
                    title: '操作成功',
                    success: _ => {
                      wx.redirectTo({
                        url: '/pages/print_list/index?type=sc&fileId=' + res.data,
                      })
                    }
                  })
                }
              }).catch(err => {
                wx.hideLoading()
                wx.showModal({
                  title: "错误",
                  content: err.msg
                })
              })


            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请上传身份证',
        mask: true,
        icon:'error'
      })
    }
  },
  jumpYinSi(){
    wx.navigateTo({
      url: '../wode/yinsi'
    })
  },
  onShareAppMessage() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let mini = res.data.mini;
        share = {
          title: mini.title,
          path: "/pages/index/index",
          imageUrl: mini.image,
        }
      }
    })
    return share;
  },
  popBtnTap(res) {
    console.log("授权结果返回数据 :>> ", res);
    console.log("授权结果 :>> ", res.detail);
    if (res.detail.result) {
      wx.showToast({
        title: "同意授权",
        icon: "success",
      });
    } else {
      wx.reLaunch({
        url: "../index/index"
      });
    }
  },
  onShareTimeline() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let momnet = res.data.momnet;
        share = {
          title: momnet.title,
          query: {
            printer_id: this.data.printer.id || 0
          },
          imageUrl: momnet.image,
        }
      }
    })
    return share;
  }
})